package com.lukk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import com.lukk.constant.Const;
import com.lukk.constant.PayMethod;
import com.lukk.constant.WorkerWalletRecordType;
import com.lukk.dao.CityCostRuleDAO;
import com.lukk.dao.MeasureIndentDAO;
import com.lukk.dao.ProvinceDAO;
import com.lukk.dao.WorkerDAO;
import com.lukk.dao.WorkerWalletRecordDAO;
import com.lukk.pojo.CityCostRule;
import com.lukk.pojo.Indent;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.Province;
import com.lukk.pojo.Worker;
import com.lukk.pojo.WorkerWalletRecord;
import com.lukk.service.IMeasureIndentService;
import com.lukk.util.DateUtils;
import com.lukk.util.JsonUtil;
import com.lukk.util.LuKeOrderNoUtil;
import com.lukk.util.jpush.JPushUtil;

/**
 * 
 * MeasureIndentServiceImpl 上午10:25:11
 * 
 * Copyright zhaocj Inc. All rights reserved.
 * Love ME Like Justin Bieber.
 */
@Service("measureIndentService")
public class MeasureIndentServiceImpl implements IMeasureIndentService {
	private static Logger logger = LogManager.getLogger(MeasureIndentServiceImpl.class.getName());
	@Resource
	private MeasureIndentDAO measureIndentDAO;
	@Resource
	private ProvinceDAO provinceDAO;
	@Resource
	private WorkerDAO workerDAO;
	@Resource
	private WorkerWalletRecordDAO workerWalletRecordDAO;
	@Resource
	private CityCostRuleDAO cityCostRuleDAO;

	@Override
	public Map<String, Object> selectWorkerIndentByPage(Integer offset, Integer limit, String sort, String order,
			String merchantName, String phoneNumber, Integer indentStatus, String beginTime, String endTime,
			Integer indentID, HttpSession session) {
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		List<Province> provincesForInternalWorker = provinceDAO.selectAvailableProvinces(internalWorker);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Map<String, Object> retsMap = new HashMap<String, Object>();
		if (provincesForInternalWorker.isEmpty()) {
			retsMap.put("total", 0);
			retsMap.put("rows", new ArrayList<>());
			return retsMap;
		} else {
			queryMap.put("offset", offset);
			queryMap.put("limit", limit);
			queryMap.put("sort", sort);
			queryMap.put("order", order);
			queryMap.put("merchantName", merchantName);
			queryMap.put("phoneNumber", phoneNumber);
			queryMap.put("indentStatus", indentStatus);
			queryMap.put("beginTime", beginTime);
			queryMap.put("endTime", endTime);
			queryMap.put("indentID", indentID);
			queryMap.put("provinces", provincesForInternalWorker);
			List<Indent> beans = measureIndentDAO.selectWorkerIndentByMap(queryMap);
			retsMap.put("total", measureIndentDAO.selectWorkerIndentCountByMap(queryMap));
			retsMap.put("rows", beans);
			return retsMap;
		}
	}

	@Override
	public Map<String, Object> selectMerchantIndentByPage(Integer offset, Integer limit, String sort, String order,
			String merchantName, String phoneNumber, Integer indentStatus, String beginTime, String endTime,
			Integer indentID, HttpSession session) {
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Map<String, Object> retsMap = new HashMap<String, Object>();
		// 查询出来每一个员工所分配的商人的总数
		queryMap.put("offset", offset);
		queryMap.put("limit", limit);
		queryMap.put("sort", sort);
		queryMap.put("order", order);
		queryMap.put("merchantName", merchantName);
		queryMap.put("phoneNumber", phoneNumber);
		queryMap.put("indentStatus", indentStatus);
		queryMap.put("beginTime", beginTime);
		queryMap.put("endTime", endTime);
		queryMap.put("indentID", indentID);
		queryMap.put("internalWorkerID", internalWorker.getId());
		List<Indent> beans = measureIndentDAO.selectMerchantIndentByMap(queryMap);
		retsMap.put("total", measureIndentDAO.selectMerchantIndentCountByMap(queryMap));
		retsMap.put("rows", beans);
		return retsMap;
	}

	@Override
	public boolean modifyIndentStatus(Integer indentID, Integer status) {
		Integer modifyNumber = measureIndentDAO.modifyIndentStatus(indentID, status);
		logger.debug("修改了{}行", modifyNumber);
		return true;
	}

	@Override
	public boolean modifyForReallocatedIndent(Integer indentID, Integer workerID, Integer indentStatus) {
		// 初始化订单信息
		Integer modifyNumber = measureIndentDAO.initialMeasureIndentToAcceptable(indentID);
		logger.debug("修改了{}行", modifyNumber);
		// 更改员工钱包额度
		WorkerWalletRecord workerWalletRecord = new WorkerWalletRecord();
		Worker worker = workerDAO.selectByPrimaryKey(workerID);
		logger.debug(JsonUtil.toJson(worker));
		// 得到城市的code
		logger.debug("该工人位于所在城市扣款信息：    "
				+ JsonUtil.toJson(cityCostRuleDAO.selectCostRuleByCityCode(worker.getWorkerSpaceCodeStr(), 0)));
		CityCostRule cityCostRule = cityCostRuleDAO.selectCostRuleByCityCode(worker.getWorkerSpaceCodeStr(), 0);
		Double workerWallet = workerDAO.selectWalletByWorkerID(workerID);
		logger.debug("查询出来工人的money:{}",workerWallet);
		Double recordMainMoney = 0.0;
		if (indentStatus == 1) {
			workerWalletRecord.setRemark("已接测量订单违约取消扣款。");
			workerWallet = workerWallet - cityCostRule.getPunishWorker();
			recordMainMoney = cityCostRule.getPunishWorker();
		} else {
			workerWalletRecord.setRemark("出发测量订单违约取消扣款。");
			workerWallet = workerWallet - cityCostRule.getPunishClient();
			recordMainMoney =  cityCostRule.getPunishClient();
		}
		logger.debug("workerID：{}",workerID);
		modifyNumber = workerDAO.modifyWorkerWalletByWorkerID(workerID,workerWallet);
		workerWalletRecord.setMainMoney(recordMainMoney);
		workerWalletRecord.setWorkerId(workerID);
		workerWalletRecord.setStatus(1);
		workerWalletRecord.setOrderNo(LuKeOrderNoUtil.getWorkerOrderNo(workerID, PayMethod.BALABCEPAY.getIndex(),
				WorkerWalletRecordType.BREAK_CONTRACT.getIndex()));
		workerWalletRecord.setIndentId(indentID);
		workerWalletRecord.setRecordBeginTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		workerWalletRecord.setType(WorkerWalletRecordType.BREAK_CONTRACT.getIndex());
		workerWalletRecordDAO.insertReAllocateMoneyRecord(workerWalletRecord);
		logger.debug("修改了{}行", modifyNumber);
		Map<String, Object> indentMap = measureIndentDAO.selectIndentByIndentIDForJpush(indentID);
		Integer roomNumB = (Integer) indentMap.get("roomNumB");
		indentMap.put("roomNumB", roomNumB == null ? 0 : roomNumB);
		JPushUtil.JpushIndent(indentMap);// 通过极光推送订单
		return true;
	}
}
